<?php
    namespace util\account;

    use \artisan\input;
    use \artisan\cache;
    class access
    {

        //允许错误的次数
        public static $err_num = 5;

        //KEY前缀
        public static $key_pre = 'ERROR_RECORD_ACCESS_';

        //是否验证来源IP
        public static $check_ip = false;

        //自定义IP
        public static $access_ip;

        //错误保存时间
        public static $save_time = 5;

        //是否验证来源是否为机器人 内部接口不可用
        public static $enable_robot_check = false;

        //当前错误类型 robot 机器人 lock 已锁定
        public static $err_type = 'lock';

        //当前KEY 用于调试
        private static $_now_key;

        //错误次数
        private static $_err_num = 1;

        //验证是否为机器人
        static function checkAccessIsRobot($internal = false)
        {

            $object = new \soa\http\agent();

            $isRobot = $object->is_robot();

            if($internal)
                return $isRobot;
            else
                if($isRobot)
                {
                    header('Content-type:text/html;charset=utf-8');
                    echo $val = '请求非法,请通过正常途径访问!';
                    exit();
                }

            return false;
        }

        //验证是否允许访问
        static function checkAccessIsAllow($unique_key = '' , $use = '')
        {

            //验证来源是否为机器人
            if(self::$enable_robot_check)
            {
                return self::checkAccessIsRobot(true);
            }

            //获取生成的KEY
            $key = self::getSaveKey($unique_key , $use);
            if($key == false)
                return false;

            //获取缓存内容
            $cache  = (int)cache::get($key)?:0;

            //次数相加
            if($cache)
                self::$_err_num += $cache;

            //锁定
            if(self::$_err_num >= self::$err_num)
                return false;

            return true;
        }

        //得带本次运行的相关参数 用于调试
        static function getParams()
        {
            return [
                'now_key'=>self::$_now_key,
                'key_pre'=>self::$key_pre,
                'err_num'=>self::$err_num,
                'check_ip'=>self::$check_ip,
                'access_ip'=>self::$access_ip,
                'save_time'=>self::$save_time,
                'enable_robot_check'=>self::$enable_robot_check
            ];
        }

        //得到错误次数
        static function getErrNum()
        {
            return self::$_err_num;
        }

        //记录错误
        static function setErrorAccess($unique_key = '', $use = '')
        {
            $key = self::getSaveKey($unique_key , $use);

            if($key == false)
                return false;

            $cache = (int)cache::get($key)?:0;

            cache::set($key,$cache + 1 , self::$save_time * 60);
        }

        //成功后重置
        static function resetError($unique_key = '' , $use = '')
        {
            cache::del(self::getSaveKey($unique_key , $use ));
        }

        //生成KEY
        private static function getSaveKey($unique_key = '', $use = '')
        {
            if(self::$check_ip)
                self::$_now_key = self::$key_pre.(self::$access_ip ? : input::ip()).'_';
            else
                self::$_now_key = self::$key_pre;

            if(empty($unique_key) || empty($use))
                return false;
            else
                return self::$_now_key .= $use.'_'.$unique_key;
        }
    }